[Python] 之旅第6天
實用練習
練習1- 用戶給半徑,要有程式能"算出面積",並"比較"兩個圓的大小
用 Flask 來與網頁互動port=5000
指port端口設定為 5000@app.route('/data')
指之後要去我們寫的file 要輸入以下
http://127.0.0.1:5000/data?
(ps. 在?後就可以開始輸入值囉)
# import main Flask class and request object
from flask import Flask, request
# create the Flask app
app = Flask(__name__)
@app.route('/data')
def query_example():
# if key doesn't exist, returns None
pi = request.args['pi']
# if key doesn't exist, returns a 400, bad request error
c1 = request.args['c1']
c2 = request.args['c2']
c1_float = float(c1)
c2_float = float(c2)
pi_float = float(pi)
multiply_c1 = c1_float*c1_float*pi_float
multiply_c2 = c2_float*c2_float*pi_float
if multiply_c1 > multiply_c2:
bigger = "c1"
elif multiply_c2 > multiply_c1:
bigger = "c2"
elif multiply_c2 == multiply_c1:
bigger = "both"
return '''
<h1>The pi is: {}</h1>
<h1>The c1_float is: {}</h1>
<h1>The c2_float is: {}</h1>
<h1>The multiply_c1 is: {}</h1>
<h1>The multiply_c2 is: {}</h1>
<h1>The bigger one is: {}</h1>
'''.format(pi, c1_float,c2_float , multiply_c1, multiply_c2 ,bigger)
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)
成果會長這樣喔:
公式- 日期相減 日期差的核心觀念/代碼:
https://blog.csdn.net/Gordennizaicunzai/article/details/78926255
from datetime import date, datetime, time ,timedelta
valentine = date(2020, 2, 14)
today = date.today()
romanticlen = today - valentine
print(romanticlen)
成果會長這樣喔:
練習2- 給兩個日期 做請假系統,看有沒有超過 limit天數
import datetime
from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date, datetime, time ,timedelta
app = Flask(__name__)
@app.route('/data')
def query_example():
try:
date1 = request.args['date1']
date2 = request.args['date2']
limit = 2
date1_in_system = datetime.strptime( date1, "%Y-%m-%d")
date2_in_system = datetime.strptime( date2, "%Y-%m-%d")
daysremain = abs((date2_in_system - date1_in_system).days)
status = 0
if daysremain > limit:
status = "失敗,請超過兩天"
if daysremain <= limit:
status = "可以請假"
except Exception as e:
return '''
<h1>捕捉錯誤資訊: {}</h1>
'''.format(date1+ str(e))
return '''
<h1>The date1_in_system is: {}</h1>
<h1>The date2_in_system is: {}</h1>
<h1>The 請假: {}</h1>
'''.format(date1_in_system, date2_in_system , status)
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)
成果會長這樣喔:
其他貼文~~
(變強,就從小小的累積開始)
[Python] 之旅第1天 - python環境
https://ithelp.ithome.com.tw/articles/10296280
[Python] 之旅第2天-用 [Python] 跟 [Flask] 為基礎,把 URL input 抓出來
https://ithelp.ithome.com.tw/articles/10296290
[Python] 之旅第3天- 用 [Python] 跟 [Flask]
https://ithelp.ithome.com.tw/articles/10296965
[Python] 之旅第4天- [Python] [Flask] 的應用
https://ithelp.ithome.com.tw/articles/10303948
[Python] 之旅第5天- [Python] [Flask] 的應用, 日期計算 及日期報錯篇
https://ithelp.ithome.com.tw/articles/10309202
[Python] 之旅第6天- [Python] [Flask] 的應用, 比較有小數點圓的面積大小,簡易請假系統製作
https://ithelp.ithome.com.tw/articles/10309352
[Python] 之旅第7天- [Python] [regular expression] 的應用,找特定資料,或限制輸入的格式
https://ithelp.ithome.com.tw/articles/10309371
[Python] 之旅第8天 - [Python] [regular expression] 的應用, 甚麼是 Json
https://ithelp.ithome.com.tw/articles/10309422
[Python] 之旅第9天 - [Python] [Json] 進階說明
https://ithelp.ithome.com.tw/articles/10309620